package io.renren.modules.sys.web_api.controller;

import io.renren.modules.sys.back.service.BtCtBaseService;
import io.renren.modules.sys.web_api.service.FirstPageService;
import io.renren.modules.sys.web_api.service.ProductPageService;
import io.renren.modules.sys.web_api.service.WendaPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("api/product")
public class WebProductController {
    @Autowired
    private BtCtBaseService btCtBaseService;
    @Autowired
    private ProductPageService productPageService;
    @Autowired
    private FirstPageService firstPageService;
    @Autowired
    private WendaPageService wendaPageService;
    @RequestMapping("/catag/{pid}")
    public String CrawlerFirst(@PathVariable("pid") Long pid ,Map<String,Object> backdata){
        if(pid<0){
            return "404";
        }
        //推荐产品
        //推荐产品
        List<Map<String,Object>> tuijian = firstPageService.firstPageTuiJian();
        backdata.put("tuijian",tuijian);

        List<Map<String,Object>> fenlei = firstPageService.firstPageProFenLei();
        backdata.put("fenlei",fenlei);
        if(pid >0){
            List<Map<String,Object>> erjifenlei = productPageService.getSubProductErJi(pid);
            backdata.put("erjifenlei",erjifenlei);
            List<Map<String,Object>> subprolist = productPageService.getSubProductList((Long)erjifenlei.get(0).get("erjiid"));
            backdata.put("subprolist",subprolist);
            backdata.put("fenleiname",productPageService.getcatagname(pid).getName());
            backdata.put("fenleilink","/api/product/catag/"+productPageService.getcatagname(pid).getId());
        }else{
            List<Map<String,Object>> subprolist = productPageService.getSubProductList(5L);
            backdata.put("subprolist",subprolist);

        }
        return "webpage/products";
    }
    @RequestMapping("/subcatag/{id}")
    public String Subcatag(@PathVariable("id") Long id ,Map<String,Object> backdata){
        if(id<0){
            return "404";
        }
        if(productPageService.getcatagname(id) == null){
            return "404";
        }
        //推荐产品
        List<Map<String,Object>> tuijian = firstPageService.firstPageTuiJian();
        backdata.put("tuijian",tuijian);
        List<Map<String,Object>> fenlei = firstPageService.firstPageProFenLei();
        backdata.put("fenlei",fenlei);
        //二级分类
        List<Map<String,Object>> erjifenlei = productPageService.getSubProductErJi(productPageService.getcatagname(id).getPid());
        backdata.put("erjifenlei",erjifenlei);

        backdata.put("fenleiname",productPageService.getcatagname(productPageService.getcatagname(id).getPid()).getName());
        backdata.put("fenleilink","/api/product/catag/"+productPageService.getcatagname(productPageService.getcatagname(id).getPid()).getId());
        List<Map<String,Object>> subprolist = productPageService.getSubProductList(id);
        backdata.put("subprolist",subprolist);
        backdata.put("sanjiname",productPageService.getcatagname(id).getName());
        backdata.put("sanjilink","/api/product/subcatag/"+productPageService.getcatagname(id).getId());

        return "webpage/products";
    }

    @RequestMapping("/detail/{id}")
    public String ProductDetail(@PathVariable("id") Long id ,Map<String,Object> backdata){
        if(id<0){
            return "404";
        }
        if(btCtBaseService.selectById(id)==null){
            return "404";
        }
        List<Map<String,Object>> fenlei = firstPageService.firstPageProFenLei();
        backdata.put("fenlei",fenlei);
        Map<String,Object> detailinfo = productPageService.getProductDtailInfo(id);
        backdata.put("detailinfo",detailinfo);
        List<Map<String,Object>> wenda = wendaPageService.getAllWenda();
        backdata.put("wenda",wenda);
        return "webpage/producdetail";
    }
}
